package com.dtjd.playcoinmonkey.activities;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.view.MenuItem;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import com.dtjd.playcoinmonkey.R;
import com.dtjd.playcoinmonkey.bean.HttpParaBean;
import com.dtjd.playcoinmonkey.databinding.ActivityFindPasswordBinding;
import com.dtjd.playcoinmonkey.utils.Network;
import com.dtjd.playcoinmonkey.utils.Tools;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class FindPasswordActivity extends AppCompatActivity {
    int time=60;
    Handler handler=new Handler();
    Runnable runnable=new Runnable(){
        @SuppressLint("SetTextI18n")
        @Override
        public void run() {
            if (time>0){
                time--;
                handler.postDelayed(this, 1000);
                binding.findSend.setText(time+"s");
            }else {
                time=60;
                binding.findSend.setEnabled(true);
                binding.findSend.setText(getResources().getString(R.string.sendEmail));
            }

        }
    };
    private ActivityFindPasswordBinding binding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityFindPasswordBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        setTitle(getResources().getString(R.string.find_password));
        ActionBar actionbar = getSupportActionBar();
        if (actionbar!=null){
            actionbar.setDisplayHomeAsUpEnabled(true);
            actionbar.setHomeButtonEnabled(true);
        }
        setResult(2);
        binding.findSend.setOnClickListener(view -> {
            String email=binding.findEmail.getText().toString();
            if (email.equals("")){
                Tools.toast( getResources().getString(R.string.input_email));
                return;
            }




            List<HttpParaBean> para=new ArrayList<>();
            para.add(new HttpParaBean("email",binding.findEmail.getText().toString()));
            Network.post(para, "wbhMain/getUserInfo", new Callback() {
                @Override
                public void onFailure(@NonNull Call call, @NonNull IOException e) {
                    e.printStackTrace();
                }
                @Override
                public void onResponse(@NonNull Call call, @NonNull Response response) {
                    runOnUiThread(() -> {
                        try {
                            JsonObject jo = JsonParser.parseString(Objects.requireNonNull(response.body()).string()).getAsJsonObject();
                            if (jo.has("datalist")){
                                handler.postDelayed(runnable,0);
                                binding.findSend.setEnabled(false);
                                ArrayList<HttpParaBean> list = new ArrayList<>();
                                list.add(new HttpParaBean("mail",email));
                                list.add(new HttpParaBean("type","pwd"));
                                Network.post(list,"wbh/code", new Callback() {
                                    @Override
                                    public void onFailure(@NonNull Call call, @NonNull IOException e) {
                                        e.printStackTrace();
                                    }
                                    @Override
                                    public void onResponse(@NonNull Call call, @NonNull Response response) {
                                        runOnUiThread(() -> {
                                            try {
                                                Tools.toast( new JSONObject(response.body().string()).getString("datalist"));
                                            }catch (Exception e){
                                                e.printStackTrace();
                                            }
                                        });
                                    }
                                });
                            }else {
                                Tools.toast( getResources().getString(R.string.userNotExist));
                            }
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                    });
                }
            });
        });
        binding.findModify.setOnClickListener(view -> {
            String p = binding.findPassword.getText().toString();
            String p1 = binding.findPassword2.getText().toString();
            if (p.equals("")){
                Tools.toast( getResources().getString(R.string.input_new_password));
                return;
            }
            if (p1.equals("")){
                Tools.toast( getResources().getString(R.string.input_password2));
                return;
            }
            if (!p.equals(p1)){
                Tools.toast( getResources().getString(R.string.password_not_match));
                return;
            }
            if (!Tools.passwordCorrect(p)){
                Tools.toast( getResources().getString(R.string.set_password)+getResources().getString(R.string.password_dec));
                return;
            }
            ArrayList<HttpParaBean>list=new ArrayList<>();
            list.add(new HttpParaBean("verifyCode", binding.findVcode.getText().toString()));
            list.add(new HttpParaBean("mail",binding.findEmail.getText().toString()));
            list.add(new HttpParaBean("newPassword",p));
            Network.post(list, "wbh/forgetPassword", new Callback() {
                @Override
                public void onFailure(@NonNull Call call, @NonNull IOException e) {
                    e.printStackTrace();
                }
                @Override
                public void onResponse(@NonNull Call call, @NonNull Response response) {
                    runOnUiThread(() -> {
                        try {
                            JSONObject jo = new JSONObject(response.body().string());
                        if (jo.getInt("code")==2){
                            setResult(3);
                            finish();
                        }else {
                            Tools.toast( jo.getString("datalist"));
                        }
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                    });
                }
            });
        });
    }
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (android.R.id.home==item.getItemId()){ finish(); }
        return super.onOptionsItemSelected(item);
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeCallbacks(runnable);
    }
}